﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using PmsClient;

namespace web.ClientMgr
{
    public partial class Default : System.Web.UI.Page
    {
        //private bool ibnew = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Button_Save.Enabled = true;
                HiddenField_ClientID.Value = null;
                PageControls.textbox_readonly_false(this.UpdatePanel_ClientInfo);
                //PageControls.textbox_nvl(this.UpdatePanel_ClientInfo);
                txtPersonBirthday_CalendarExtender.Enabled = true;
                txtPersonCardActiveDate_CalendarExtender.Enabled = true;
                UpdatePanel_ClientInfo.Update();
                SetFocus(this.txtClientName);
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
          //1  TextBox1.Text = GridView1.SelectedRow.Cells[3].Text;
          //2  TextBox1.Text = GridView1.SelectedDataKey[0].ToString();
          //TextBox1.Text = GridView1.SelectedValue.ToString();
            
            DataSet idataset;
            try{
                idataset = DbHelperSQL.Query("SELECT * FROM ClientOfClient where clientid=" + int.Parse(GridView_ClientList.SelectedValue.ToString()) +" ");
            }catch (Exception ee)
            {
                LogService.Write("ClientMgr.Default查询客户信息失败！");
                LogService.Write(ee.Message);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('查询客户信息失败！')", true);
                return;
            }
            
            HiddenField_ClientID.Value = GridView_ClientList.SelectedValue.ToString();

            txtClientName.Text = idataset.Tables[0].Rows[0]["CLIENTNAME"].ToString();
            txtPersonSex.Text = idataset.Tables[0].Rows[0]["PERSONSEX"].ToString();
            txtPersonBirthday.Text = ((DateTime)idataset.Tables[0].Rows[0]["PERSONBIRTHDAY"]).ToString("yyyy-MM-dd");
            txtPersonNation.Text = idataset.Tables[0].Rows[0]["PERSONNATION"].ToString();
            txtPersonEduDegree.Text = idataset.Tables[0].Rows[0]["PERSONEDUDEGREE"].ToString();
            txtPersonMerriageState.Text = idataset.Tables[0].Rows[0]["PERSONMERRIAGESTATE"].ToString();
            txtPersonIDCardNO.Text = idataset.Tables[0].Rows[0]["PERSONIDCARDNO"].ToString();
            txtPersonCardActiveDate.Text = ((DateTime)idataset.Tables[0].Rows[0]["PERSONCARDACTIVEDATE"]).ToString("yyyy-MM-dd");
            txtPersonRegisterPlace.Text = idataset.Tables[0].Rows[0]["PERSONREGISTERPLACE"].ToString();
            txtPersonIDCardAddress.Text = idataset.Tables[0].Rows[0]["PERSONIDCARDADDRESS"].ToString();
            txtPersonOtherCardName.Text = idataset.Tables[0].Rows[0]["PERSONOTHERCARDNAME"].ToString();
            txtPersonOtherCardNo.Text = idataset.Tables[0].Rows[0]["PERSONOTHERCARDNO"].ToString();
            txtPersonCorpName.Text = idataset.Tables[0].Rows[0]["PERSONCORPNAME"].ToString();
            txtPersonProfession.Text = idataset.Tables[0].Rows[0]["PERSONPROFESSION"].ToString();
            txtClientCurTel.Text = idataset.Tables[0].Rows[0]["CLIENTCURTEL"].ToString();
            txtClientCurAddr.Text = idataset.Tables[0].Rows[0]["CLIENTCURADDR"].ToString();
            txtPersonIsHouseMaster.Text = idataset.Tables[0].Rows[0]["PERSONISHOUSEMASTER"].ToString();
            txtClientNO.Text = idataset.Tables[0].Rows[0]["CLIENTNO"].ToString();
            txtClientDesp.Text = idataset.Tables[0].Rows[0]["CLIENTDESP"].ToString();
            txtTreeID.Text = idataset.Tables[0].Rows[0]["TREEID"].ToString();

            GridView_Rela.DataSource = null;
            GridView_Rela.DataSourceID = "SqlDataSource_Rela";
            GridView_Rela.DataBind();
            GridView_Rela.ShowFooter = false;
            if (GridView_Rela.Rows.Count == 0)
            {
                DataTable dt = new DataTable();
                //dt.Columns.Add("RelaTypeID");
                //dt.Columns.Add("RelaTypeNO");
                //dt.Columns.Add("RelaTypeName");
                //dt.Columns.Add("ClientID");
                dt.Columns.Add("");
                dt.Columns.Add("RelaTypeID");
                dt.Columns.Add("RelaTypeNO");
                dt.Columns.Add("ImportExplain");
                dt.Columns.Add("RelaIsActive");
                dt.Columns.Add("IsPrint");
                dt.Columns.Add("PrintKind");
                dt.Columns.Add("RelaTypeName");
                dt.Columns.Add("RelaContect");
                dt.Columns.Add("Postalcode");
                dt.Columns.Add("RelaTypeDesp");
                PmsClient.Gridviewnvl.GridViewDataBind(this.GridView_Rela, dt);
            }
            
        }

        protected void Button_Add_Click(object sender, EventArgs e)
        {
            HiddenField_ClientID.Value = null;
            PageControls.textbox_readonly_false(this.UpdatePanel_ClientInfo);
            PageControls.textbox_nvl(this.UpdatePanel_ClientInfo);
            txtPersonBirthday_CalendarExtender.Enabled = true;
            txtPersonCardActiveDate_CalendarExtender.Enabled = true;
            UpdatePanel_ClientInfo.Update();
            SetFocus(this.txtClientName);
            //PageControls.button_enable_false(this.UpdatePanel_CMD);
            //Button_Save.Enabled = true;
            //Button_Cancel.Enabled = true;
        }

        protected void Button_Modify_Click(object sender, EventArgs e)
        {
            //PageControls.textbox_readonly_false(this.UpdatePanel_ClientInfo);
            //txtPersonBirthday_CalendarExtender.Enabled = true;
            //txtPersonCardActiveDate_CalendarExtender.Enabled = true;
            //UpdatePanel_ClientInfo.Update();
            //SetFocus(this.txtClientName);
            //PageControls.button_enable_false(this.UpdatePanel_CMD);
            //Button_Save.Enabled = true;
            //Button_Cancel.Enabled = true;
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            string strErr = "";

            if (this.txtClientNO.Text == "")
            {
                strErr += "ClientNO不能为空！\\n";
            }
            if (this.txtClientName.Text == "")
            {
                strErr += "ClientName不能为空！\\n";
            }
            if (this.txtClientDesp.Text == "")
            {
                strErr += "ClientDesp不能为空！\\n";
            }
            if (this.txtClientCurTel.Text == "")
            {
                strErr += "ClientCurTel不能为空！\\n";
            }
            if (this.txtClientCurAddr.Text == "")
            {
                strErr += "ClientCurAddr不能为空！\\n";
            }
            if (this.txtPersonSex.Text == "")
            {
                strErr += "PersonSex不能为空！\\n";
            }
            if (this.txtPersonNation.Text == "")
            {
                strErr += "PersonNation不能为空！\\n";
            }
            if (!PageValidate.IsDateTime(txtPersonBirthday.Text))
            {
                strErr += "PersonBirthday不是时间格式！\\n";
            }
            if (this.txtPersonEduDegree.Text == "")
            {
                strErr += "PersonEduDegree不能为空！\\n";
            }
            if (this.txtPersonMerriageState.Text == "")
            {
                strErr += "PersonMerriageState不能为空！\\n";
            }
            if (this.txtPersonIDCardNO.Text == "")
            {
                strErr += "PersonIDCardNO不能为空！\\n";
            }
            if (!PageValidate.IsDateTime(txtPersonCardActiveDate.Text))
            {
                strErr += "PersonCardActiveDate不是时间格式！\\n";
            }
            if (this.txtPersonRegisterPlace.Text == "")
            {
                strErr += "PersonRegisterPlace不能为空！\\n";
            }
            if (this.txtPersonIDCardAddress.Text == "")
            {
                strErr += "PersonIDCardAddress不能为空！\\n";
            }
            if (this.txtPersonOtherCardName.Text == "")
            {
                strErr += "PersonOtherCardName不能为空！\\n";
            }
            if (this.txtPersonOtherCardNo.Text == "")
            {
                strErr += "PersonOtherCardNo不能为空！\\n";
            }
            if (this.txtPersonCorpName.Text == "")
            {
                strErr += "PersonCorpName不能为空！\\n";
            }
            if (this.txtPersonProfession.Text == "")
            {
                strErr += "PersonProfession不能为空！\\n";
            }
            if (this.txtPersonIsHouseMaster.Text == "")
            {
                strErr += "PersonIsHouseMaster不能为空！\\n";
            }
            if (!PageValidate.IsNumber(txtTreeID.Text))
            {
                strErr += "TreeID不是数字！\\n";
            }
            if (strErr != "")
            {
                MessageBox.Show(UpdatePanel_ClientInfo, strErr);
                //ScriptManager.RegisterStartupScript(UpdatePanel_ClientInfo, UpdatePanel_ClientInfo.GetType(), "", "alert('" + strErr + "!')", true);
                PageControls.textbox_readonly_false(this.UpdatePanel_ClientInfo);
                txtPersonBirthday_CalendarExtender.Enabled = true;
                txtPersonCardActiveDate_CalendarExtender.Enabled = true;
                UpdatePanel_ClientInfo.Update();
                SetFocus(this.txtClientName);
                PageControls.button_enable_false(this.UpdatePanel_CMD);
                Button_Save.Enabled = true;
                //Button_Cancel.Enabled = true;
                return;
            }
            { 
                string ClientNO = this.txtClientNO.Text;
                string ClientName = this.txtClientName.Text;
                string ClientDesp = this.txtClientDesp.Text;
                string ClientCurTel = this.txtClientCurTel.Text;
                string ClientCurAddr = this.txtClientCurAddr.Text;
                string PersonSex = this.txtPersonSex.Text;
                string PersonNation = this.txtPersonNation.Text;
                DateTime PersonBirthday = DateTime.Parse(this.txtPersonBirthday.Text);
                string PersonEduDegree = this.txtPersonEduDegree.Text;
                string PersonMerriageState = this.txtPersonMerriageState.Text;
                string PersonIDCardNO = this.txtPersonIDCardNO.Text;
                DateTime PersonCardActiveDate = DateTime.Parse(this.txtPersonCardActiveDate.Text);
                string PersonRegisterPlace = this.txtPersonRegisterPlace.Text;
                string PersonIDCardAddress = this.txtPersonIDCardAddress.Text;
                string PersonOtherCardName = this.txtPersonOtherCardName.Text;
                string PersonOtherCardNo = this.txtPersonOtherCardNo.Text;
                string PersonCorpName = this.txtPersonCorpName.Text;
                string PersonProfession = this.txtPersonProfession.Text;
                string PersonIsHouseMaster = this.txtPersonIsHouseMaster.Text;
                int TreeID = int.Parse(this.txtTreeID.SelectedValue);

                PmsClient.Model.ClientOfClient model = new PmsClient.Model.ClientOfClient();

                model.ClientNO = ClientNO;
                model.ClientName = ClientName;
                model.ClientDesp = ClientDesp;
                model.ClientCurTel = ClientCurTel;
                model.ClientCurAddr = ClientCurAddr;
                model.PersonSex = PersonSex;
                model.PersonNation = PersonNation;
                model.PersonBirthday = PersonBirthday;
                model.PersonEduDegree = PersonEduDegree;
                model.PersonMerriageState = PersonMerriageState;
                model.PersonIDCardNO = PersonIDCardNO;
                model.PersonCardActiveDate = PersonCardActiveDate;
                model.PersonRegisterPlace = PersonRegisterPlace;
                model.PersonIDCardAddress = PersonIDCardAddress;
                model.PersonOtherCardName = PersonOtherCardName;
                model.PersonOtherCardNo = PersonOtherCardNo;
                model.PersonCorpName = PersonCorpName;
                model.PersonProfession = PersonProfession;
                model.PersonIsHouseMaster = PersonIsHouseMaster;
                model.TreeID = TreeID;

                int ret_ClientID;
                if (HiddenField_ClientID.Value == null || HiddenField_ClientID.Value == "")
                {
                    PmsClient.BLL.ClientOfClient bll_INSERT = new PmsClient.BLL.ClientOfClient();
                    ret_ClientID = bll_INSERT.Add(model);
                    if (ret_ClientID > 0)
                    {
                        HiddenField_ClientID.Value = ret_ClientID.ToString();
                    }
                    else
                    {
                        MessageBox.Show(UpdatePanel_CMD, "新增数据失败！");
                    }
                }
                else
                {
                    model.ClientID = int.Parse(HiddenField_ClientID.Value);
                    PmsClient.BLL.ClientOfClient bll_UPDATE = new PmsClient.BLL.ClientOfClient();
                    bll_UPDATE.Update(model); 
                }

                //PageControls.textbox_readonly_true(this.UpdatePanel_ClientInfo);
                //txtPersonBirthday_CalendarExtender.Enabled = false;
                //txtPersonCardActiveDate_CalendarExtender.Enabled = false;
                //UpdatePanel_ClientInfo.Update();
                //PageControls.button_enable_true(this.UpdatePanel_CMD);
                //Button_Save.Enabled = false;
                //Button_Cancel.Enabled = false;

                SqlDataSource_ClientList.DataBind();
                GridView_ClientList.DataBind();
                UpdatePanel_ClientList.Update();
            }

        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {
            PageControls.textbox_readonly_true(this.UpdatePanel_ClientInfo);
            txtPersonBirthday_CalendarExtender.Enabled = false;
            txtPersonCardActiveDate_CalendarExtender.Enabled = false;
            UpdatePanel_ClientInfo.Update();
            PageControls.button_enable_true(this.UpdatePanel_CMD);
            Button_Save.Enabled = false;
            //Button_Cancel.Enabled = false;
        }

        protected void DropDownList_ClientTree_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdatePanel_ClientList.Update();
        }

        protected void GridView_Member_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //GridView_Member.DataSourceID = "";
            //GridView_Member.DataBind();

        }

        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            //GridView_Member.DataSourceID = "SqlDataSource_Member";
            //GridView_Member.DataBind();

        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            GridView_Rela.ShowFooter = false;
        }

        protected void LinkButton_relaadd_Click(object sender, EventArgs e)
        {
            //DataTable dt = new DataTable();
            //dt.Columns.Add("RelaTypeID");
            //dt.Columns.Add("RelaTypeNO");
            //dt.Columns.Add("ImportExplain");
            //dt.Columns.Add("RelaIsActive");
            //dt.Columns.Add("IsPrint");
            //dt.Columns.Add("PrintKind");
            //dt.Columns.Add("RelaTypeName");
            //dt.Columns.Add("RelaContect");
            //dt.Columns.Add("Postalcode");
            //dt.Columns.Add("RelaTypeDesp");
            //if (dt.Rows.Count == 0)
            //{
            //    dt.Rows.Add(dt.NewRow());
            //}
            //if (GridView_Rela.Rows.Count == 0 || GridView_Rela.Rows[0].Cells[0].Text == "没有信息")
            //{
            //    this.GridView_Rela.DataSourceID = "";
            //    this.GridView_Rela.DataSource = dt;
            //    this.GridView_Rela.DataBind();
                
              

            ////    //GridView_Rela. .Columns[0].ed .ItemTemplate.LinkButton1.visible = false;
            ////    //int columnCount = dt.Columns.Count;
            ////    //GridView_Rela.Rows[0].Cells.Clear();
            ////    //GridView_Rela.Rows[0].Cells.Add(new TableCell());
            ////    //GridView_Rela.Rows[0].Cells[0].ColumnSpan = columnCount;
            ////    //GridView_Rela.Rows[0].Cells[0].Text = "没有记录";
            ////    //GridView_Rela.Rows[0].Cells[0].Style.Add("text-align", "center");
            ////    //GridView_Rela.DataBind();
            ////    //GridView_Rela.PageIndex = GridView_Rela.PageCount - 1;
            //}
            
            //((DataTable)GridView_Rela.DataSource).Rows.Add(((DataTable)GridView_Rela.DataSource).NewRow());
            //if (GridView_Rela.DataSourceID == "SqlDataSource_Rela")
            //{
            GridView_Rela.ShowFooter = true;
            //}
            //else
            //{
            //    //PmsClient.Gridviewnvl.GridViewDataBind(this.GridView_Rela, SqlDataSource_Rela.);
            //}

            //this.GridView_Rela.DataSourceID = "SqlDataSource_Rela";
            //this.GridView_Rela.DataSource = null;
            //this.GridView_Rela.DataBind();

        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            ////TextBox customerID = GridView_Rela.FooterRow.FindControl("CustomerIDTextBox") as TextBox;
            ////TextBox companyName = GridView_Rela.FooterRow.FindControl("CompanyNameTextBox") as TextBox;
            //DropDownList RelaTypeName = GridView_Rela.FooterRow.FindControl("DropDownListBox_RelaTypeName") as DropDownList;
            ////SqlDataSource_Rela.InsertParameters["CustomerID"].DefaultValue = customerID.Text;
            ////SqlDataSource_Rela.InsertParameters["CompanyName"].DefaultValue = companyName.Text;
            //SqlDataSource_Rela.InsertParameters["RelaTypeName"].DefaultValue = RelaTypeName.SelectedValue;
            ////SqlDataSource_Rela.InsertParameters["ClientID"].DefaultValue = HiddenField_ClientID.Value;
            //SqlDataSource_Rela.Insert();
            //GridView_Rela.ShowFooter = false;
            //this.GridView_Rela.DataSource = null;
            //this.GridView_Rela.DataSourceID = "SqlDataSource_Rela";
            //this.GridView_Rela.DataBind();

        }

        protected void GridView_Rela_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            ////if (e.Row.RowIndex > -1)     //这两种写法都可以；
            //{
            //    e.Row.Visible = false;
            //}
            ////if (GridView_Rela.Rows.Count == 0)
            ////{
            ////    e.Row.Visible = false;
            ////}

        }

        protected void GridView_Rela_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView_Rela_Load(object sender, EventArgs e)
        {
            //this.GridView_Rela.DataSource = null;
            //this.GridView_Rela.DataSourceID = "SqlDataSource_Rela";
            //this.GridView_Rela.DataBind();

        }

        protected void GridView_Rela_Init(object sender, EventArgs e)
        {
            
        }

        protected void GridView_Rela_DataBound(object sender, EventArgs e)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //GridView_Rela.ShowFooter = true;
        }

    }
}
